//
//  CNKISuggestViewController.m
//  CNKICommunity
//
//  Created by wei on 17/3/16.
//  Copyright © 2017年 CNKI. All rights reserved.
//

#import "CNKISuggestViewController.h"
#define Search_LeftMargin 10//默认左边距
#define Search_TopMargin 10//默认上边距
@interface CNKISuggestViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,assign)UIEdgeInsets originalContentInset;//记录原始的位置



@end

@implementation CNKISuggestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboradFrameDidChange:) name:UIKeyboardDidShowNotification object:nil];
    
    
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)keyboradFrameDidChange:(NSNotification*)noti
{
    //出现搜索建议后，点击了某个搜索建议，跳转到下一界面，返回后，搜索建议也要更新
    [self setSearchSuggestion:_searchSuggestion];
}

-(void)setSearchSuggestion:(NSArray<NSString *> *)searchSuggestion
{
    _searchSuggestion = searchSuggestion;
    [self.tableView reloadData];
    //self.tableView.contentOffset = CGPointMake(0, 0);
    
}
#pragma mark--tableViewdelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _searchSuggestion.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"CNKISearchResultCellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.textLabel.textColor = TitleTagColor;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.backgroundColor = [UIColor clearColor];
        
        //添加分割线
        UIImageView *lineView = [[UIImageView alloc]init];
        lineView.image = [UIImage imageNamed:@"cell-content-line"];
        lineView.x = Search_LeftMargin;
        lineView.width = cell.width - Search_LeftMargin;
        lineView.y = cell.height - 1;
        lineView.height = 1;
        [cell.contentView addSubview:lineView];
        
    }
    cell.textLabel.text = self.searchSuggestion[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"search_history"];
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.SuggestBlock)
    {
        self.SuggestBlock([tableView cellForRowAtIndexPath:indexPath]);
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
